using System;
using System.Collections;
using System.IO;
using UnityEngine;

namespace PKGames.AssetBundleSystem
{
	public class AssetBundleManager : MonoSingleton<AssetBundleManager>
	{
		private string _assetBundleVersion = string.Empty;

		private string _baseAssetBundleUrl = string.Empty;

		[SerializeField]
		private bool _isDisabledBundles;

		[SerializeField]
		private bool _isEmbeddedBundles;

		private readonly AssetBundleManagerHelper _helper = new AssetBundleManagerHelper();

		public bool IsDisabledBundles => _isDisabledBundles;

		public bool IsEmbeddedBundles => _isEmbeddedBundles;

		public string AssetBundleVersion => _assetBundleVersion;

		public string LocalAssetBundleBasePath
		{
			get
			{
				if (_isEmbeddedBundles)
				{
					return EmbeddedAssetBundleBasePath;
				}
				return "Resources/";
			}
		}

		public string EmbeddedAssetBundleBasePath =>  "Resources/";

		public string RemoteAssetBundleBasePath => string.Format("{0}/{1}/{2}/", _baseAssetBundleUrl, "Android", _assetBundleVersion);

		protected override void Awake()
		{
			base.Awake();
			_helper.Init();
			CreateLocalAssetBundlesDirectory();
			AssetBundleSetupResolverInterface assetBundleSetupResolverInterface = AssetBundleSetupResolverFactory.CreateAssetBundleSetupResolver();
			_assetBundleVersion = assetBundleSetupResolverInterface.GetAssetBundleVersion();
			_baseAssetBundleUrl = assetBundleSetupResolverInterface.GetAssetBundleHost();
			UnityEngine.Debug.Log(base.Tag + "AssetBundleURL: " + RemoteAssetBundleBasePath);
		}

		private void CreateLocalAssetBundlesDirectory()
		{
			if (!IsEmbeddedBundles && !Directory.Exists(LocalAssetBundleBasePath))
			{
				Directory.CreateDirectory(LocalAssetBundleBasePath);
			}
		}

		protected override void OnDestroy()
		{
			base.OnDestroy();
			_helper.DeInit();
		}

		public IEnumerator LoadLocalAssetBundle(string assetBundleName, bool embedded, Action<float> progressCallback, Action onFinished)
        {
            //UnityEngine.Resources.LoadAll()
            //yield break;
            if (_isDisabledBundles || AssetBundleCache.IsAssetBundleCached(assetBundleName))
			{
				onFinished.Fire();
				yield break;
            }
            string path = (!embedded) ? LocalAssetBundleBasePath : EmbeddedAssetBundleBasePath;
			//if (path.StartsWith("jar", StringComparison.CurrentCulture))
			//{
				string fullPath = path + assetBundleName;
            UnityEngine.Object[] objects = UnityEngine.Resources.LoadAll(assetBundleName);
            AssetBundleCache.CacheAssetBundle(assetBundleName, objects);
                //AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(fullPath);
                //yield return request;
                //AssetBundle assetBundle = request.assetBundle;
                //if (assetBundle == null)
                //{
                //	UnityEngine.Debug.Log("Failed to load: " + assetBundleName);
                //}
                //else
                //{
                //	AssetBundleCache.CacheAssetBundle(assetBundleName, assetBundle);
                //}
                //}
                //else
                //{
                //	AssetBundleLoadRequest assetBundleLoadRequest = new AssetBundleLoadRequest(assetBundleName, path, progressCallback);
                //	Coroutine loadCoroutine = new Coroutine(assetBundleLoadRequest);
                //	yield return loadCoroutine.Start(this);
                //	loadCoroutine.CheckError();
                //}
            onFinished.Fire();
		}

		public void UnloadBundle(string assetBundleName)
		{
			AssetBundleCache.UnloadAssetBundle(assetBundleName);
		}

		public void UnloadAllAssetBundles()
		{
			AssetBundleCache.UnloadAll();
		}
	}
}
